<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将行格式化为 CSV 并写入文件指针</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.fpassthru.html">« fpassthru</a></li>
      <li style="float: right;"><a href="function.fputs.html">fputs »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>将行格式化为 CSV 并写入文件指针</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.fputcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fputcsv</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fputcsv</span> &mdash; <span class="dc-title">将行格式化为 CSV 并写入文件指针</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fputcsv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fputcsv</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">array</span> <code class="parameter">$fields</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$separator</code><span class="initializer"> = &quot;,&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &quot;\&quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$escape</code><span class="initializer"> = &quot;\\&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$eol</code><span class="initializer"> = &quot;\n&quot;</span></span><br>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>fputcsv()</strong></span> 将一行（用
   <code class="parameter">fields</code> 数组传递）格式化为 CSV
   格式并写入由 <code class="parameter">stream</code>
   指定的文件。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fputcsv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">文件指针必须是有效的，必须指向由
<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 或 <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> 成功打开的文件(并还未由 <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span> 关闭)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">fields</code></dt>

     <dd>

      <p class="para">
       <span class="type">string</span> 数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">separator</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">separator</code>
       参数设定字段分界符（只允许一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enclosure</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">enclosure</code>
       参数设定字段字段环绕符（只允许一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">escape</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">escape</code>
       参数设置转义字符（只允许一个单字节字符）。空字符串（<code class="literal">&quot;&quot;</code>）禁用专有转义机制。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">eol</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">eol</code> 参数设置自定义行尾序列。
      </p>
     </dd>

    
   </dl>

  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    If an <code class="parameter">enclosure</code> character is contained in a field,
    it will be escaped by doubling it, unless it is immediately preceded by an
    <code class="parameter">escape</code>.
   </p>
  </p></blockquote> 
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fputcsv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回写入字符串的长度， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fputcsv-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.1.0</td>
       <td>
        新增可选的 <code class="parameter">eol</code> 参数。
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        <code class="parameter">escape</code> 参数现在接受空字符串来禁用专有转义机制。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fputcsv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2178">
    <p><strong>示例 #1 <span class="function"><strong>fputcsv()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$list&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'aaa'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bbb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ccc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dddd'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'456'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'789'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'"aaa"'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'"bbb"'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'file.csv'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$list&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$fields</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fputcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例子会写入以下的<code class="literal">file.csv</code>：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
aaa,bbb,ccc,dddd
123,456,789
&quot;&quot;&quot;aaa&quot;&quot;&quot;,&quot;&quot;&quot;bbb&quot;&quot;&quot;

</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fputcsv-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时， 如果 PHP
不能正确的识别行结束符，启用运行时配置可选项 <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
也许可以解决此问题。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fputcsv-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fgetcsv.html" class="function" rel="rdfs-seeAlso">fgetcsv()</a> - 从文件指针中读入一行并解析 CSV 字段</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>